package com.egg.common;

import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;
/**
 * @项目名称：egg   
 * @类名称：AuthorityInterceptor   
 * @类描述：权限拦截器   
 * @创建人：wangchenyang
 * @创建时间：2012-9-4 下午3:13:31   
 * @修改人：
 * @修改时间：   
 * @修改备注：   
 * @version
 */
public class CopyOfAuthorityInterceptor extends MethodFilterInterceptor {
	private static final long serialVersionUID = 2855071154376065828L;

	@SuppressWarnings("unchecked")
	@Override
	protected String doIntercept(ActionInvocation invocation) throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession(true);
		// 当前访问的url请求
		String presentPath = request.getRequestURI();
		String userId = null;
		if (session != null) {
			userId = String.valueOf(session.getAttribute("userId"));
		}
		List<Map<String, Object>> list = (List<Map<String, Object>>) session.getAttribute("menu");
		if (userId != null && !"".equals(userId) && list != null
				&& list.size() != 0) {
			for (int i = 0; i < list.size(); i++) {
				Map<String, Object> map = list.get(i);
				String mapValue = String.valueOf(map.get("MENU_URL"));
				//如果找到对应的值说明当前用户有权限访问此菜单
				if (mapValue.equals(presentPath)) {
					return invocation.invoke();
				}
			}
		}
		return Action.LOGIN;
	}

}
